package com.logic.system;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 
 *
 * 项目名称：system-server</br>
 * 类名称：AppContext</br>
 * 类描述：应用上下文</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年7月5日 下午5:47:23</br>
 * @version 1.0
 *
 */
@Component
public class AppContext implements ApplicationContextAware
{
    private static final Logger LOG = LoggerFactory.getLogger(AppContext.class);
    
    private ApplicationContext applicationContext;  
    
    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
        throws BeansException
    {
        this.applicationContext = applicationContext;
    }
    
    /**
     * 
     * [简要描述]：根据beanId获取bean</br>
     * [详细描述]：bean不存在返回null</br>
     * [作者]：Aaron(2017-07-05)</br>
     *
     * @param beanName beanId
     * @return bean对象
     *
     */
    public Object getBean(String beanName)
    {  
        try
        {
            return this.applicationContext.getBean(beanName);  
        }
        catch (Exception e)
        {
            LOG.error("getBean|get bean failed with beanName=" + beanName);
            return null;
        }
    }
}
